package almata.dai.jhuguet;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.Iterator;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

/**
 * Permet consulta les sales de la base de dades.
 * 
 * @author Joan Ramon
 * 
 */

public class PantallaConsultaSala  {

	private Sala sala;
	private DefaultTableModel dtmPublicacions;
	private JTable tblPublicacions;
	private JScrollPane jspPublicacions;
	private Gestor gestor;
	private JButton btnSortir;
	private PantallaInicial pantallaInicial;
	private JInternalFrame consultaInterna;
	private JDesktopPane desktoppane;
	private Collection<Sala> sales;
	
	
	public PantallaConsultaSala(PantallaInicial unaPantallaInicial){
		pantallaInicial= unaPantallaInicial;
		inicialitza();
		activarMenus(false);
		crearComponents();
		crearGestorsDisseny();
		sales=carregaSales();
		getSales();
		
	}
	
	private void inicialitza(){
		gestor= new Gestor();
	}
	
	private Collection<Sala> carregaSales(){
		sala= new Sala(null, null, null, 0, null);
		GestorBD ges = new GestorBD();
		return ges.obtenirSala();
	}
	
	private void crearComponents(){
		
		//TAULA DADES PUBLICACIONS no Editable
		dtmPublicacions= new DefaultTableModel(){
			@Override     
			public boolean isCellEditable (int fila, int columna) {               
				return false;      
			}
		};
		//afegir columnes
		dtmPublicacions.addColumn("Codi Sala");
		dtmPublicacions.addColumn("Nom");
		dtmPublicacions.addColumn("Descripcio");
		dtmPublicacions.addColumn("Metres Quatrats");
		dtmPublicacions.addColumn("Nom del Centre");
		tblPublicacions= new JTable(dtmPublicacions);
		//Si activo (ordenacio) la consulta d'Autors falla"!!!!!!!!!
		//tblPublicacions.setAutoCreateRowSorter(true);
		//no permetre interncanivar l'order de les columnes
		tblPublicacions.getTableHeader().setReorderingAllowed(false) ;
		jspPublicacions= new JScrollPane(tblPublicacions);
		
		
		//boto acceptar
		btnSortir= new JButton("Sortir");
		btnSortir.addActionListener(gestor);
		btnSortir.setActionCommand("Sortir");
	}
	
	private void crearGestorsDisseny(){
		//crear desktopPane
		desktoppane= new JDesktopPane();
		//crear frameIntern
		consultaInterna= new JInternalFrame("Consulta Sales");
		
		//afegir frameInern al desktoppane
		desktoppane.add(consultaInterna);
		
		//crear gestor de posicionament del frameIntern
		GridBagLayout gbl= new GridBagLayout();
		GridBagConstraints gbc= new GridBagConstraints();
		consultaInterna.setLayout(gbl);
		
		//taula Centres
		gbc.gridx=0;
		gbc.gridy=5;
		gbc.gridwidth=GridBagConstraints.REMAINDER;
		gbc.fill=GridBagConstraints.BOTH;
		gbc.weightx=1;
		gbc.weighty=1;
		gbl.setConstraints(jspPublicacions, gbc);
		consultaInterna.add(jspPublicacions);
		//boto Sortir
		gbc.gridx=0;
		gbc.gridy=6;
		gbc.gridwidth=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.EAST;
		gbc.weightx=0;
		gbc.weighty=0;
		gbl.setConstraints(btnSortir, gbc);
		consultaInterna.add(btnSortir);
		
		//titol del frame INTERN
		TitledBorder tb=BorderFactory.createTitledBorder("CONSULTES");
		tb.setTitleColor(Color.BLUE);
		consultaInterna.setBorder(tb);
	
		//Afegir frameIntern al JFrame
		pantallaInicial.getContentPane().add(consultaInterna,BorderLayout.CENTER);
		//visualitzar frame intern
		consultaInterna.setVisible(true);
	}
	
	private void getSales(){
	
		Iterator<Sala> iterSala = sales.iterator();
		while(iterSala.hasNext()){
			Sala sala = iterSala.next();
			Object oP[]= new Object[5];
			oP[0]=sala.getCodi();
			oP[1]=sala.getNom();
			oP[2]=sala.getDescripcio();
			oP[3]=sala.getMetresQuadrats();
			oP[4]=sala.getNom_centre();
			dtmPublicacions.addRow(oP);
			
		}

	}
	public class Gestor implements ActionListener{
		
		public void actionPerformed(ActionEvent arg0) {
		
			Object o= arg0.getSource();
			if(o instanceof JButton){
				JButton boto= (JButton) o;
				if(boto.getActionCommand().equals("Sortir")){
					activarMenus(true);
					consultaInterna.dispose();
					
				}
			}
		}
	}
	
	private void activarMenus(Boolean activar){
		pantallaInicial.getMenuCentre().setEnabled(activar);
		pantallaInicial.getMenuSales().setEnabled(activar);
		pantallaInicial.getMenuAparells().setEnabled(activar);
	}

}